package db4o;

import java.util.HashSet;
import java.util.Set;

import pojo.Device;
import pojo.EDeviceState;
import pojo.EDeviceType;

import com.db4o.ObjectSet;

public class DeviceRetriver extends DBManager {

	private ObjectSet<Device> result;

	public Device fetchDevice(String description) {
		result = super.getDbManager().queryByExample(
				new Device(description, null));
		if (!result.isEmpty())
			return result.next();
		else
			return null;
	}
	
	public Device fetchDevice(Integer id) {
		result = super.getDbManager().queryByExample(
				new Device(id,null,null));
		if (!result.isEmpty())
			return result.next();
		else
			return null;
	}

	public Set<Device> getAllDevices() {
		Set<Device> devices = new HashSet<Device>();
		result = super.getDbManager().queryByExample(Device.class);
		for (Device d : result) {
			devices.add(d);
		}
		return devices;
	}

	public Set<Device> getWaitingDevice() {
		Set<Device> devices = new HashSet<Device>();
		for (Device d : this.getAllDevices()) {
			if (d.getState().equals(EDeviceState.WAITING)) {
				devices.add(d);
			}
		}
		return devices;

	}

	public static void main(String[] args) {
		DeviceCreator creator = new DeviceCreator();
		creator.devicePersist(new Device("Spsuty gladzik", EDeviceType.LAPTOP));
		creator.devicePersist(new Device("Grzeje sie", EDeviceType.LAPTOP));
		creator.devicePersist(new Device("USB nie swieci", EDeviceType.LAPTOP));
		
	}
}
